package com.article.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.article.entity.Article;
import com.article.entity.Editor;
import com.article.jsonfactory.ConvertJsonUtils;
import com.article.service.IArticleService;
import com.article.service.ICategoryService;
import com.article.service.IEditorService;

@SuppressWarnings("serial")
public class ArticleAction extends BaseAction implements ServletRequestAware {
	private IArticleService articleService;
	private IEditorService editorService;
	private ICategoryService categoryService;
	private HttpServletRequest request;
	private Article article;
	private String cateId;
	
	/**
	 * 添加文章
	 * @return
	 */
	public String add(){
		//User user = (User) request.getSession().getAttribute("user");
		//Editor editor = editorService.findByUserName(user.getUserName());
		//原本应该是用上面两行代码查找当前登录用户为作者的,为了测试方便就不登录了
		Editor editor = editorService.findByUserName("tsingheng");
		article.setEditor(editor);
		article.setCategory(categoryService.findById(cateId));
		boolean result = articleService.addArticle(article);
		outJson("{success:" + result + ",errorMsg:\"添加失败！请重新添加或联系管理员！\"}");
		return null;
	}
	
	/**
	 * 修改文章
	 * @return
	 */
	public String edit(){
		String id = request.getParameter("id");
		article.setId(Integer.parseInt(id));
		article.setCategory(categoryService.findById(cateId));
		boolean result = articleService.editArticle(article);
		outJson("{success:" + result + ",errorMsg:\"修改失败！请重新修改或联系管理员！\"}");
		return null;
	}
	
	/**
	 * 删除文章
	 * @return
	 */
	public String del(){
		String id = request.getParameter("id");
		boolean result = articleService.deleteArticle(id);
		outJson("{success:" + result + ",errorMsg:\"删除失败！请重新删除或联系管理员！\"}");
		return null;
	}

	/**
	 * 查询所有文章并分页显示
	 * 
	 * @return
	 */
	public String showAllByPage() {
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		List<Article> list = articleService.findAllByPage(start, limit);
		String json = ConvertJsonUtils.convertListToPageJson(list,
				articleService.findAllCount());
		outJson(json);
		return null;
	}

	/**
	 * 根据文章标题模糊查询并分页显示
	 * 
	 * @return
	 */
	public String showByTitle() {
		String title = request.getParameter("title");
		try {
			title = java.net.URLDecoder.decode(title, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("titleKeyWords:" + title);
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		List<Article> list = articleService.findByKeyWordsByPage(title,
				start, limit);
		String json = ConvertJsonUtils.convertListToPageJson(list,
				articleService.findCountByKeyWords(title));
		outJson(json);
		return null;
	}

	/**
	 * 根据文章类别查询文章列表并分类显示
	 * 
	 * @return
	 */
	public String showByCate() {
		String cateId = request.getParameter("cateId");
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		List<Article> list = articleService.findByCateByPage(cateId, start,
				limit);
		String json = ConvertJsonUtils.convertListToPageJson(list,
				articleService.findCountByCate(cateId));
		outJson(json);
		return null;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public void setArticleService(IArticleService articleService) {
		this.articleService = articleService;
	}

	public void setEditorService(IEditorService editorService) {
		this.editorService = editorService;
	}

	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public Article getArticle() {
		return article;
	}

	public String getCateId() {
		return cateId;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public void setCateId(String cateId) {
		this.cateId = cateId;
	}

}
